import { defineStore } from "pinia";
import { ref } from "vue";
import type { Ref } from "vue";

import type { OrderDetailVO, OrderInfoVO, OrderItemVO, OrderStatusDTO, SubmitOrderDTO } from "@/types/orderType";
import { cancelOrderAPI, deleteOrderAPI, fetchOrderInfoAPI, fetchOrderListAPI, fetchOrderLogisticsAPI, fetchOrderNowInfoAPI, findOrderDetailAPI, findWxPaymentParamsAPI, mockPaymentAPI, receiveAPI, sendAPI, submitOrderAPI } from "@/api/orderAPI";

const useOrderStore = defineStore("orderStore", () => {
   // 订单列表
   const orderList: Ref<OrderItemVO[]> = ref<OrderItemVO[]>([]);
   // 订单总数
   const total: Ref<number> = ref<number>(1);
   // 订单预支付信息
   const orderInfo: Ref<OrderInfoVO> = ref<OrderInfoVO>({} as OrderInfoVO);
   // 订单详情
   const orderDetail: Ref<OrderDetailVO> = ref({} as OrderDetailVO);
   // 发送请求获取订单列表的回调函数
   const fetchOrderList = async (data: OrderStatusDTO) => {
      try {
         if (orderList.value.length >= total.value) {
            await uni.showToast({ title: "数据加载完毕", icon: "success" });
         } else {
            const { result } = await fetchOrderListAPI(data);
            orderList.value.push(...result.items);
            total.value = result.counts;
            data.page += 1;
         }
      } catch (error: any) {
         await error;
      }
   };
   // 发送请求删除订单的回调函数
   const deleteOrder = async (ids: string[]) => {
      try {
         await deleteOrderAPI(ids);
      } catch (error: any) {
         await error;
      }
   };
   // 发送请求取消订单的回调函数
   const cancelOrder = async (id: string, data: { cancelReason: string }) => {
      try {
         await cancelOrderAPI(id, data);
      } catch (error: any) {
         await error;
      }
   };
   // 发送请求确认收货的回调函数
   const receive = async (id: string) => {
      try {
         await receiveAPI(id);
      } catch (error: any) {
         await error;
      }
   };
   // 发送请求模拟发货的回调函数
   const send = async (id: string) => {
      try {
         await sendAPI(id);
      } catch (error: any) {
         await error;
      }
   };
   //发送获取预付订单信息的回调函数
   const fetchOrderInfo = async () => {
      try {
         const { result } = await fetchOrderInfoAPI();
         orderInfo.value = result;
      } catch (error: any) {
         await error;
      }
   };
   //发送获取立即购买订单信息的回调函数
   const fetchOrderNowInfo = async (data: { skuId?: string, count?: number, addressId?: string }) => {
      try {
         const { result } = await fetchOrderNowInfoAPI(data);
         orderInfo.value = result;
      } catch (error: any) {
         await error;
      }
   };
   // 发送请求提交订单的回调函数
   const submitOrder = async (data: SubmitOrderDTO) => {
      try {
         const { result } = await submitOrderAPI(data);
         return result.id;
      } catch (error: any) {
         await error;
      }
   };
   // 发送请求获取订单详情的回调函数
   const findOrderDetail = async (id: string) => {
      try {
         const { result } = await findOrderDetailAPI(id);
         orderDetail.value = result;
      } catch (error: any) {
         await error;
      }
   };
   // 获取订单物流
   const fetchOrderLogistics = async (id: string) => {
      try {
         const { result } = await fetchOrderLogisticsAPI(id);
         console.log(result);
      } catch (error: any) {
         await error;
      }
   };
   // 模拟支付
   const mockPayment = async (id: string) => {
      try {
         await mockPaymentAPI(id);
      } catch (error: any) {
         await error;
      }
   };
   // 获取微信支付参数的接口
   const findWxPaymentParams = async (id: string) => {
      try {
         const { result } = await findWxPaymentParamsAPI(id);
         return result;
      } catch (error: any) {
         await error;
      }
   };
   return {
      orderList,
      total,
      orderInfo,
      orderDetail,
      fetchOrderList,
      deleteOrder,
      cancelOrder,
      receive,
      send,
      fetchOrderInfo,
      fetchOrderNowInfo,
      submitOrder,
      findOrderDetail,
      fetchOrderLogistics,
      mockPayment,
      findWxPaymentParams,

   };
});

export default useOrderStore;